from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QTabWidget
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QAction
from PyQt5.QtWidgets import qApp
from PHCryptoView import CryptWidget
from PHCryptoView import CTFWidget
from PHAndroidView import AndroidRemoteHook
from PHAndroidView import AndroidCapture
from PHAndroidView import AndroidRPC
from PHAndroidView import AndroidFileOption
from PHAndroidView import FridaOption
from PHAnalysisAppView import AnalysisAppWidget
import sys
import configparser
import os


class LogDialog(QDialog):
    text_edit = None

    def __init__(self):
        super(LogDialog, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.text_edit = QTextEdit()
        self.text_edit.setFontFamily('Consolas')
        self.text_edit.setFontPointSize(12)
        self.text_edit.setReadOnly(True)

        clear_button = QPushButton('清空')
        clear_button.clicked.connect(self.clear)

        v_box = QVBoxLayout()
        v_box.addWidget(self.text_edit)
        v_box.addWidget(clear_button)
        self.setLayout(v_box)
        self.setWindowTitle("日志输出")
        self.setGeometry(1000, 400, 800, 700)

    def append(self, text):
        self.text_edit.append(text)

    def error(self):
        pass

    def info(self):
        pass

    def debug(self):
        pass

    def clear(self):
        self.text_edit.clear()


class MainWin(QMainWindow):
    log_view = None

    def __init__(self):
        super(MainWin, self).__init__()
        self.init_ui()

    def init_ui(self):

        self.log_view = LogDialog()

        exit_action = QAction('&退出', self)
        exit_action.setShortcut('Ctrl+Q')
        exit_action.setStatusTip('退出程序')
        exit_action.triggered.connect(qApp.quit)

        log_action = QAction('&日志', self)
        log_action.setShortcut('Ctrl+R')
        log_action.setStatusTip('打开日志')
        log_action.triggered.connect(self.open_log_view)

        menu_bar = self.menuBar()
        tools = menu_bar.addMenu('&文件操作')
        tools.addAction(log_action)
        tools.addAction(exit_action)

        start_server32 = QAction('&启动frida32服务', self)
        start_server64 = QAction('&启动frida64服务', self)
        # start_server.setStatusTip('打开日志')
        # log_action.triggered.connect(self.open_log_view)

        frida_server = menu_bar.addMenu('&Frida服务')
        frida_server.addAction(start_server32)
        frida_server.addAction(start_server64)

        tab = QTabWidget()
        tab.addTab(AndroidRemoteHook(self.log_view), '安卓挂钩工具')
        tab.addTab(AnalysisAppWidget(self.log_view), 'App静态分析工具')
        tab.addTab(CryptWidget(self.log_view), '常用加解密工具')
        tab.addTab(CTFWidget(self.log_view), 'CTF加解密工具')
        tab.addTab(AndroidCapture(self.log_view), '肉丝姐的抓包工具')
        tab.addTab(AndroidFileOption(self.log_view), '安卓文件操作工具')

        self.setCentralWidget(tab)
        self.setGeometry(900, 300, 800, 700)
        self.setWindowTitle('胖虎的工具箱')
        self.show()

    def open_log_view(self):
        self.log_view.show()


if __name__ == '__main__':

    try:
        # config = os.getcwd() + '/config/config.ini'
        # conf = configparser.ConfigParser()
        # conf.read(config, encoding="utf-8")  # python3
        # sections = conf.sections()
        # items = conf.items(sections[0])

        app = QApplication(sys.argv)
        ex = MainWin()
        sys.exit(app.exec_())

    except Exception as e:
        print(e)
